<?php

/**
 * uniBoard API Permission Mask
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-15 17:35:38 +0000 (Wed, 15 Sep 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_permission_mask extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * Mask ID
	 *
	 * @var integer
	 */
	
	public $mask_id 				= 0;
	
	/**
	 * Mask Name
	 *
	 * @var string
	 */
	
	public $mask_name 				= NULL;
	
	/**
	 * Mask Permissions
	 *
	 * @var array
	 */
	
	public $mask_permissions		= array();
					
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store Mask ID
		$this -> mask_id	= (integer) $target_id;
		
		// Autoload If Mask ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'permission_masks
			WHERE mask_id = \'' . $this -> mask_id . '\'
			LIMIT 1');
			
			// Mask found
			if ( $mask_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded		= true;
				
				$this -> mask_name		 		= $mask_result['mask_name'];
				$this -> mask_permissions		= (array) unserialize( $mask_result['mask_permissions']);
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_mask_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> mask_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'permission_masks');
			
				// Store new mask id
				$this -> mask_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'permission_masks', 'mask_id = \'' . $this -> mask_id . '\'');
			}
		
			// Force perms rebuild?
			if ( isset( $this -> changes['mask_permissions']))
			{
				$this -> appCore -> db  -> update( array('user_acl_cache' => ''), 'users');
			}
			
			// Flush cache
			class_cache::flushCache( 'masks');
			class_cache::flushCache( 'permission_masks');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Delete API data to DB
	 *
	 */
	
	function deleteData(){
		
		// Delete data
		$this -> appCore -> db -> delete( 'permission_masks', 'mask_id = \'' . $this -> mask_id . '\'');
		
		// Flush cache
		class_cache::flushCache( 'masks');
		class_cache::flushCache( 'permission_masks');
		
		// Flush perms
		$this -> appCore -> db -> update( array( 'user_acl_cache' => ''), 'users');
		$this -> appCore -> db -> update( array( 'setting_value' => ''), 'settings', 'setting_id = \'crawlers_acl_cache\'');
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set mask_name
	 *
	 * @param string $new_value
	 */
	
	function setMaskName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> mask_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_mask_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['mask_name'] = $this -> appCore -> db -> strEscape( $this -> mask_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_mask_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set permission_masks
	 *
	 * @param array $new_value
	 */
	
	function setMaskPermissions( $new_value){
	
		// Store in object
		$this -> mask_permissions = (array) $new_value;
		
		// Add to made changes
		$this -> changes['mask_permissions'] = $this -> appCore -> db -> strEscape( serialize( $this -> mask_permissions));

	}
		
}